home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / duftp / ls2filel.c < prev    next >
Text File  |  1995-06-15  |  3KB  |  142 lines

  1. /*
  2.     DUFTP
  3. */
  4.  
  5. // 'ls -l' listing to file_list conversion
  6.  
  7. #include <DULIB.H>
  8. #include <STDLIB.H>
  9. #include <STDIO.H>
  10. #include "ls2filel.h"
  11. #include "globals.h"
  12.  
  13. file_list *ls_to_list(char *filename)
  14. {
  15.     char buffer[500];
  16.     char *buf_ptr;
  17.     char buf2[FMSIZE];
  18.     FILE *f;
  19.     file_list *flist;
  20.     file_list *rtn_list;
  21.     file_list *flist_c;
  22.     
  23.     f=fopen(filename,"r");
  24.     if (f)
  25.     {
  26.         fgets(buffer,499,f);        // Skip the header line 'total n'
  27.         rtn_list=flist=NULL;
  28.         
  29.         while (feof(f)==0)
  30.         {
  31.             flist_c=(file_list*)malloc(sizeof(file_list));
  32.             
  33.             fgets(buffer,499,f);        // Read file line
  34.             
  35.             buf_ptr=buffer;
  36.             
  37.             if (!feof(f))
  38.             {
  39.                 if ((buf_ptr[0]=='d')    // Is this a directory?
  40.                     ||((((buf_ptr[0]=='l')&&(buf_ptr[3]=='x'))&&(buf_ptr[6]=='x'))&&(buf_ptr[9]=='x')))
  41.                     flist_c->is_directory=TRUE;
  42.                 else
  43.                     flist_c->is_directory=FALSE;
  44.                 
  45.                 if (buf_ptr[0]=='l')    // Is this a directory?
  46.                     flist_c->is_symlink=TRUE;
  47.                 else
  48.                     flist_c->is_symlink=FALSE;
  49.  
  50.                 if (buf_ptr[1]=='r')        // Can the owner read it?
  51.                     flist_c->owner_read=TRUE;
  52.                 else
  53.                     flist_c->owner_read=FALSE;
  54.             
  55.                 if (buf_ptr[7]=='r')        // Can the general pulic read it?
  56.                     flist_c->public_read=TRUE;
  57.                 else
  58.                     flist_c->public_read=FALSE;
  59.  
  60.                 for(buf_ptr=buf_ptr+10; *buf_ptr==' '; buf_ptr++);
  61.                 for(; *buf_ptr!=' '; buf_ptr++);
  62.                 for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
  63.                 sscanf(buf_ptr,"%s", buf2);            // Get file owner
  64.                 buf2[19]='\0';
  65.                 sprintf(flist_c->owner,"%s",buf2);
  66.                 for(buf_ptr=buf_ptr+strlen(buf2)+1; *buf_ptr==' '; buf_ptr++);
  67.                 for(; *buf_ptr!=' '; buf_ptr++);
  68.                 for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
  69.             
  70.                 sscanf(buf_ptr,"%s",buf2);            // Get file size
  71.                 flist_c->size=atol(buf2);            // convert file size into a long
  72.  
  73.                 for(buf_ptr=buf_ptr+strlen(buf2)+1; *buf_ptr==' '; buf_ptr++);
  74.                 for(; *buf_ptr!=' '; buf_ptr++);
  75.                 for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
  76.                 for(; *buf_ptr!=' '; buf_ptr++);
  77.                 for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
  78.                 for(; *buf_ptr!=' '; buf_ptr++);
  79.  
  80.                 sscanf(buf_ptr,"%s",buf2);            // get the file name
  81.                 if (flist_c->is_directory)
  82.                     sprintf(flist_c->name," %s",buf2);        // Mark directories
  83.                 else
  84.                     sprintf(flist_c->name,"  %s (%d)",buf2,flist_c->size);
  85.                 
  86.                 if (flist_c->is_symlink)            // mark symlinks so we know they're there
  87.                     flist_c->name[1]='';
  88.  
  89.                 flist_c->next=flist;
  90.                 flist=flist_c;
  91.             }else{
  92.                 free(flist_c);
  93.             }
  94.         }
  95.         fclose(f);
  96.     }
  97.  
  98.     return flist;
  99. }
  100.  
  101. char **extract_filenames(file_list *l)
  102. {
  103.     file_list *f;
  104.     char **t;
  105.     short n,cnt;
  106.     
  107.     if (!l) return NULL;
  108.     
  109.     file_count=0;
  110.     for(f=l; f!=NULL; f=f->next) file_count++;        // Count up how many files we have
  111.     cnt=file_count; if (cnt<6) cnt=6;
  112.     
  113.     t=(char**)malloc(sizeof(char*)*(cnt+2));
  114.  
  115.     n=file_count;
  116.     for(f=l; n>0; f=f->next)                        // Extract the names
  117.     {
  118.         t[n]=f->name;
  119.         n--;
  120.     }
  121.     t[0]=" ..";                // Add a parent directory entry
  122.  
  123.     if (cnt>file_count)            // Pad so we don't get empty file lists
  124.     {
  125.         for(n=file_count+1; n<cnt+1; n++)
  126.             t[n]="    ";
  127.     }
  128.     
  129.     return t;
  130. }
  131.  
  132. void dispose_file_list(file_list *f)
  133. {
  134.     file_list *n;
  135.     
  136.     while(f)
  137.     {
  138.         n=f;
  139.         f=f->next;
  140.         free(n);
  141.     }
  142. }